<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Tree;
use app\admin\model\Shpart as shpartmodel;

/**
 * 部位
 *
 * @icon fa fa-circle-o
 */
class Shpart extends Backend
{
    protected $categorylist = [];
    /**
     * Shpart模型对象
     * @var \app\admin\model\Shpart
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Shpart;
        $tree = Tree::instance();
        $tree->init(collection($this->model->order('id asc')->select())->toArray(), 'pid');
        $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
        $categorydata = [0 => ['type' => 'all', 'name' => __('None')]];
        foreach ($this->categorylist as $k => $v) {
            $categorydata[$v['id']] = $v;
        }
        
        $this->view->assign("parentList", $categorydata);

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $search = $this->request->request("search");
            //$type = $this->request->request("type");

            //构造父类select列表选项数据
            $list = [];
            $list = $this->categorylist;

            $total = count($list);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    public function addpt()
    {
        if ($this->request->isAjax()) {
            $nodes = $this->request->post();
            $list = $nodes['row'];//explode(",",$nodes);
            $leaf= explode(",", $list['rules']);
            $rs = shpartmodel::all($leaf);
            $tbs = [];
            $hd = '<table class="cms_autoformat_table" width="100%" cellspacing="1" cellpadding="4" border="0" align="center"><tbody><tr><th>项目</th><th>最低价</th><th>最高价</th><th>平均价</th></tr>';
            $ft = '</tbody></table>';
            $tbcontent = '';
            foreach($rs as $val) 
            {
                $tb = [];
                $min = explode(",",$val['min']);
                $max = explode(",",$val['max']);
                $avg = explode(",",$val['avg']);
                $tb['name'] = $val['name'];
                $tb['min'] = isset($min[1])?rand($min[0],$min[1]):"-";
                $tb['max'] = isset($max[1])?rand($max[0],$max[1]):"-";
                $tb['avg'] = isset($avg[1])?rand($avg[0],$avg[1]):"-";
                
                if($tb['min']!=="-" && $tb['max']!=="-" && $tb['avg']!=="-")
                    $tbcontent .= '<tr><td>' . $tb['name'] . '</td><td>' . $tb['min'] . '</td><td>' . $tb['max'] . '</td><td>' . $tb['avg'] . '</td></tr>';
                    //array_push($tbs, $tb);
            }
            $tbhtml = $hd . $tbcontent . $ft;
            $result = array("code"=>1, "data" => [], "text" => $tbhtml, "sign"=>1);

            return json($result);
        }
        return $this->view->fetch();
    }
    public function parttree()
    {
        $this->loadlang('shpart');

        $pt = $this->categorylist;
        foreach ($pt as $k => $v) {
            $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['name']), 'type' => 'menu', 'state' => 0);
        }
        $this->success('', null, $nodeList);
    }

}
